#include "uart.h"

void UART_Init() {
    TMOD = 0x20;      
    TH1 = 0xFD;       
    TL1 = 0xFD;
    TR1 = 1;          
    SCON = 0x50;      
    TI = 1;           
}

void UART_Send(char d) 
{
    SBUF = d;      
    while (!TI);      
    TI = 0;          
}

void UART_Interrupt(char* d) {
    if (RI) {
        RI = 0;              // 清除接收标志
        *d = SBUF; // 读取数据
    }
}

void UART_SendBuffer(unsigned char *buffer, unsigned int length) {
    unsigned int i;
    for (i = 0; i < length; i++) {
        UART_Send(buffer[i]);  
    }
}

void UART_SendUShortArray(unsigned short *buffer, unsigned int length) {
    unsigned int i;
    for (i = 0; i < length; i++) {
        UART_Send(buffer[i] & 0xFF);         
        UART_Send((buffer[i] >> 8) & 0xFF);  
    }
}

void UART_SendUIntArray(unsigned int *buffer, unsigned int length) {
    unsigned int i;
    for (i = 0; i < length; i++) {
        UART_Send(buffer[i] & 0xFF);        
        UART_Send((buffer[i] >> 8) & 0xFF);  
    }
}

void UART_SendULongArray(unsigned long *buffer, unsigned int length) {
    unsigned int i;
    for (i = 0; i < length; i++) {
        UART_Send(buffer[i] & 0xFF);         
        UART_Send((buffer[i] >> 8) & 0xFF);  
        UART_Send((buffer[i] >> 16) & 0xFF); 
        UART_Send((buffer[i] >> 24) & 0xFF); 
    }
}
